﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using TYAJZWeb.Data;
using TYAJZWeb.Utils;

namespace TYAJZWeb.Models
{
    public abstract class EquipmentBaseModel
    {
        [Display(Name = "备案编号")]
        public String Eq_no { get; set; }

        [Display(Name = "本地外地判断")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "本地外地判断不许为空")]
        public bool IsLocal { get; set; }

        [Display(Name = "设备名称")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备名称不许为空")]
        public String Eq_Name { get; set; }

        [Display(Name = "设备型号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备型号不许为空")]
        public String Eq_Type { get; set; }

        [Display(Name = "合格证号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "合格证号不许为空")]
        public String Eq_CertNo { get; set; }

        [Display(Name = "出厂日期")]
        public DateTime Eq_MadeDate { get; set; }

        [Display(Name = "出厂代号")]
        public String Eq_MadeNo { get; set; }

        [Display(Name = "产权单位")]
        public String Eq_Owner { get; set; }

        [Display(Name = "负责人")]
        public String Eq_OwnerInCharge { get; set; }

        [Display(Name = "报检号")]
        public String InspectNO { get; set; }

        //[Display(Name = "地址")]
        //public String Eq_OwnerAdderss { get; set; }

        [Display(Name = "联系电话")]
        public String Eq_OwnerPhone { get; set; }

        [Display(Name = "设备生产厂家")]
        public String Eq_Producer { get; set; }

        [Display(Name = "特种设备制造许可证编号")]
        public String Eq_SpecCertNo { get; set; }

        [Display(Name = "设备年限")]
        public String Eq_LimitMonths { get; set; }

        [Display(Name = "已用年限")]
        public String Eq_HaveUsedMonths { get; set; }

        [Display(Name = "档案位置柜号")]
        public String Eq_RecDeskNo { get; set; }

        [Display(Name = "档案位置档案盒号")]
        public String Eq_RecBoxNo { get; set; }

        [Display(Name = "设备状态")]
        public EquipmentStatus Eq_Status { get; set; }

        [Display(Name = "打印标注")]
        public bool Eq_IsPrinted { get; set; }

        [Display(Name = "录入员")]
        public String Eq_Recorder { get; set; }

        [Display(Name = "备注")]
        public String Eq_Comment { get; set; }



    }

    public enum EquipmentStatus
    {
        Available,
        Working,
        Destoryed
    }

    public class EquipmentListModel : EquipmentBaseModel
    {
        public int Id { get; set; }
        public String NO { get; set; }
        public DateTime RegisterDate { get; set; }

        public int LimitedMonth { get; set; }

        public bool IsNearlyExpire
        {
            get
            {
                return this.Eq_MadeDate.AddMonths(LimitedMonth) < DateTime.Now;
            }
        }

        public String StatusString
        {
            get
            {
                switch (this.Eq_Status)
                {
                    case EquipmentStatus.Available: return "空闲";
                    case EquipmentStatus.Destoryed: return "已报废";
                    case EquipmentStatus.Working: return "使用中";
                    default: throw new KeyNotFoundException(String.Format("{0} is invalid", this.Eq_Status.ToString()));
                }
            }
        }

        public static explicit operator EquipmentListModel(Equipment model)
        {
            EquipmentListModel m = new EquipmentListModel()
            {
                Id = model.Id,
                //NO = String.Format("{0:D5}", model.Id),
                NO = model.Eq_no,
                //Eq_OwnerAdderss = model.Eq_Adderss,
                Eq_CertNo = model.Eq_CertNo,
                Eq_Comment = model.Eq_Comment,
                Eq_HaveUsedMonths = Convert.ToInt32((DateTime.Now - model.Eq_MadeDate).TotalDays / 365).ToString(),
                Eq_OwnerInCharge = (model.IsLocal) ? model.Company.Contactor : model.Eq_InCharge,
                Eq_IsPrinted = model.Eq_IsPrinted,
                Eq_LimitMonths = Convert.ToInt32(model.LimitedMonth / 12).ToString(),
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_MadeNo = model.Eq_MadeNo,
                Eq_Name = model.EquipmentType.Name,
                Eq_Owner = (model.IsLocal) ? model.Company.Name : model.Eq_Owner,
                Eq_no = String.Format("{0:D5}", model.Id),
                Eq_Producer = (model.IsLocal) ? model.Company1.Name : model.Eq_Producer,
                Eq_RecBoxNo = model.Eq_RecBoxNo,
                Eq_RecDeskNo = model.Eq_RecDeskNo,
                Eq_Recorder = "wlifeng",
                Eq_SpecCertNo = (model.IsLocal) ? model.Company1.ProductCertNO : model.Eq_SpecCertNo,
                Eq_Status = (EquipmentStatus)Enum.Parse(typeof(EquipmentStatus), model.Eq_Status, true),
                Eq_Type = model.Eq_Type,
                RegisterDate = model.CreateDT,
                IsLocal = model.IsLocal,
                LimitedMonth = model.LimitedMonth,
                InspectNO = model.Eq_InspectNO
            };
            return m;
        }
    }

    public class EquipmentEditLocalModel
    {
        public int Id { get; set; }

        [Display(Name = "设备名称")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备名称不许为空")]
        public int Eq_Name { get; set; }

        [Display(Name = "设备型号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备型号不许为空")]
        public String Eq_Type { get; set; }

        [Display(Name = "合格证号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "合格证号不许为空")]
        public String Eq_CertNo { get; set; }

        [Display(Name = "出厂日期")]
        public DateTime Eq_MadeDate { get; set; }

        [Display(Name = "出厂代号")]
        public String Eq_MadeNo { get; set; }

        [Display(Name = "产权单位")]
        public String Eq_OwnerCompany { get; set; }

        [Required]
        public int Eq_OwnerCompanyId { get; set; }

        [Display(Name = "地址")]
        public String Eq_OwnerCompanyAddress { get; set; }

        [Display(Name = "负责人")]
        public String Eq_OwnerCompanyLegal { get; set; }

        [Display(Name = "电话")]
        public String Eq_OwnerCompanyPhone { get; set; }

        [Display(Name = "设备生产厂家")]
        public String Eq_ProducerCompany { get; set; }

        [Required(ErrorMessage = "设备生产厂家不能为空")]
        public int Eq_ProducerCompanyId { get; set; }

        [Display(Name = "特种设备制造许可证编号")]
        public String Eq_SpecCertNo { get; set; }

        [Display(Name = "备注")]
        public String Eq_Comment { get; set; }

        [Display(Name = "设计年限")]
        public int LimitedYear { get; set; }


        public static explicit operator Equipment(EquipmentEditLocalModel model)
        {
            return new Equipment()
            {
                Eq_SpecCertNo = model.Eq_SpecCertNo,
                Eq_CertNo = model.Eq_CertNo,
                Eq_Comment = model.Eq_Comment ?? String.Empty,
                Eq_InCharge = model.Eq_OwnerCompanyLegal ?? String.Empty,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_Owner = string.Empty,
                Eq_OwnerId = model.Eq_OwnerCompanyId,
                Eq_Producer = string.Empty,
                Eq_ProducerId = model.Eq_ProducerCompanyId,
                Eq_RecBoxNo = string.Empty,
                Eq_RecDeskNo = string.Empty,
                Eq_Status = EquipmentStatus.Available.ToString(),
                Eq_Name = model.Eq_Name,
                IsLocal = true,
                Id = model.Id,
                Eq_MadeNo = model.Eq_MadeNo,
                Eq_Type = model.Eq_Type,
                LimitedMonth = model.LimitedYear * 12,
                Eq_Phone = model.Eq_OwnerCompanyPhone
            };
        }

        public static explicit operator EquipmentEditLocalModel(Equipment model)
        {
            return new EquipmentEditLocalModel()
            {
                Eq_SpecCertNo = model.Eq_SpecCertNo,
                Eq_CertNo = model.Eq_CertNo,
                Eq_Comment = model.Eq_Comment,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_OwnerCompanyId = model.Eq_OwnerId ?? 0,
                Eq_ProducerCompanyId = model.Eq_ProducerId ?? 0,
                Id = model.Id,
                Eq_MadeNo = model.Eq_MadeNo,
                Eq_Type = model.Eq_Type,
                Eq_Name = model.Eq_Name,
                Eq_OwnerCompanyAddress = model.Company.Address,
                Eq_OwnerCompanyLegal = model.Company.Legal,
                Eq_OwnerCompanyPhone = model.Company.PhoneOfLegal,
                LimitedYear = model.LimitedMonth / 12,
                Eq_OwnerCompany = model.Company.Name,
                Eq_ProducerCompany = model.Company1.Name
            };
        }

    }

    public class EquipmentArchiveModel
    {
        public int Id { get; set; }

        [Display(Name = "档案位置柜号")]
        [Required(ErrorMessage = "档案位置柜号不能为空")]
        public String Eq_RecDeskNo { get; set; }

        [Display(Name = "档案位置档案盒号")]
        [Required(ErrorMessage = "档案位置档案盒号不能为空")]
        public String Eq_RecBoxNo { get; set; }

        public static explicit operator EquipmentArchiveModel(Equipment model)
        {
            return new EquipmentArchiveModel()
            {
                Id = model.Id,
                Eq_RecBoxNo = model.Eq_RecBoxNo,
                Eq_RecDeskNo = model.Eq_RecDeskNo
            };
        }
    }

    public class EquipmentEditExternalModel
    {
        public int Id { get; set; }

        [Display(Name = "设备名称")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备名称不许为空")]
        public int Eq_Name { get; set; }

        [Display(Name = "设备型号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "设备型号不许为空")]
        public String Eq_Type { get; set; }

        [Display(Name = "合格证号")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "合格证号不许为空")]
        public String Eq_CertNo { get; set; }

        [Display(Name = "出厂日期")]
        public DateTime Eq_MadeDate { get; set; }

        [Display(Name = "出厂代号")]
        public String Eq_MadeNo { get; set; }

        [Display(Name = "设备产权单位")]
        public String Eq_Owner { get; set; }

        [Display(Name = "负责人")]
        public String Eq_OwnerInCharge { get; set; }

        [Display(Name = "联系电话")]
        public String Eq_OwnerPhone { get; set; }

        [Display(Name = "设备生产厂家")]
        public String Eq_Producer { get; set; }

        [Display(Name = "设计单位")]
        public String Eq_DesignCompany { get; set; }

        [Display(Name = "备注")]
        public String Eq_Comment { get; set; }

        [Display(Name = "起重设备备案编号")]
        public String Eq_NO { get; set; }

        [Display(Name = "设备使用年限")]
        public int LimitedMonth { get; set; }


        public static explicit operator Equipment(EquipmentEditExternalModel model)
        {
            return new Equipment()
            {
                Eq_no = model.Eq_NO,
                Eq_Phone = model.Eq_OwnerPhone,
                Eq_SpecCertNo = string.Empty,
                Eq_CertNo = model.Eq_CertNo,
                Eq_Comment = model.Eq_Comment ?? String.Empty,
                Eq_InCharge = model.Eq_OwnerInCharge,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_Owner = model.Eq_Owner,
                Eq_OwnerId = null,
                Eq_Producer = model.Eq_Producer,
                Eq_ProducerId = null,
                Eq_RecBoxNo = string.Empty,
                Eq_RecDeskNo = string.Empty,
                Eq_Status = EquipmentStatus.Available.ToString(),
                Eq_Name = model.Eq_Name,
                IsLocal = false,
                Id = model.Id,
                Eq_MadeNo = model.Eq_MadeNo,
                Eq_Type = model.Eq_Type,
                Eq_DesignCompany = model.Eq_DesignCompany,
                LimitedMonth = model.LimitedMonth
            };
        }
    }

    public class EquipmentPrintLocalModel : EquipmentEditLocalModel
    {
        [Display(Name = "起重机械备案编号")]
        public String NO { get; set; }

        [Display(Name = "发证时间")]
        public DateTime RegisterDate { get; set; }

        [Display(Name = "打印机")]
        public int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print1Name;
            }
        }

        public String RegisterDateString
        {
            get
            {
                return RegisterDate.ToString("yyyy年MM月dd日");
            }
        }

        public String MadeDateString
        {
            get
            {
                return Eq_MadeDate.ToString("yyyy年MM月dd日");
            }
        }

        [Display(Name = "设备产权单位")]
        public String OwnerCompanyName { get; set; }

        [Display(Name = "地址")]
        public String OwnerCompanyAddress { get; set; }

        [Display(Name = "负责人")]
        public String OwnerCompanyInCharge { get; set; }

        [Display(Name = "联系电话")]
        public String OwnerCompanyPhone { get; set; }

        [Display(Name = "设备生产厂")]
        public String ProducerCompany { get; set; }

        [Display(Name = "起重机械名称")]
        public String Name { get; set; }


        [Display(Name = "设备管理部门")]
        public string AJZName
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Name;
                else
                    return string.Empty;
            }
        }

        [Display(Name = "地址")]
        public string AJZAddress
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Address;
                else
                    return string.Empty;
            }
        }

        [Display(Name = "联系电话")]
        public string AJZTel
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Tel;
                else
                    return string.Empty;
            }
        }

        public static explicit operator EquipmentPrintLocalModel(Equipment model)
        {
            return new EquipmentPrintLocalModel()
            {
                OwnerCompanyName = model.Company.Name,
                OwnerCompanyAddress = model.Company.Address,
                OwnerCompanyInCharge = model.Eq_InCharge,
                OwnerCompanyPhone = model.Eq_Phone,
                ProducerCompany = model.Company1.Name,
                Eq_SpecCertNo = model.Eq_SpecCertNo,
                Name = model.EquipmentType.Name,
                NO = model.Eq_no,
                Eq_Type = model.Eq_Type,
                Eq_CertNo = model.Eq_CertNo,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_MadeNo = model.Eq_MadeNo,
                RegisterDate = model.CreateDT,
                Id = model.Id
            };
        }
    }

    public class EquipmentPrintExternalModel : EquipmentEditExternalModel
    {
        [Display(Name = "起重设备名称")]
        public String Name { get; set; }

        [Display(Name = "发证时间")]
        public DateTime RegisterDate { get; set; }

        [Display(Name = "录入时间")]
        public DateTime CreateDT { get; set; }

        public String CreateDTString
        {
            get
            {
                return CreateDT.ToString("yyyy年MM月dd日");
            }
        }

        public String RegisterDateString
        {
            get
            {
                return RegisterDate.ToString("yyyy年MM月dd日");
            }
        }

        public String MadeDateString
        {
            get
            {
                return Eq_MadeDate.ToString("yyyy年MM月dd日");
            }
        }

        [Display(Name = "打印机")]
        public int PrinterId
        {
            get
            {
                return (HttpContext.Current.Application.Get(SystemVariableEnums.Print.ToString()) as PrintEditModel).Print1Name;
            }
        }

        [Display(Name = "设备管理部门")]
        public string AJZName
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Name;
                else
                    return string.Empty;
            }
        }

        [Display(Name = "地址")]
        public string AJZAddress
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Address;
                else
                    return string.Empty;
            }
        }

        [Display(Name = "联系电话")]
        public string AJZTel
        {
            get
            {
                if (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] != null)
                    return (HttpContext.Current.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel).Tel;
                else
                    return string.Empty;
            }
        }

        public static explicit operator EquipmentPrintExternalModel(Equipment model)
        {
            return new EquipmentPrintExternalModel()
            {
                Eq_Owner = model.Eq_Owner,
                Eq_OwnerInCharge = model.Eq_InCharge,
                Eq_OwnerPhone = model.Eq_Phone,
                Eq_Producer = model.Eq_Producer,
                Eq_DesignCompany = model.Eq_DesignCompany,
                Name = model.EquipmentType.Name,
                Eq_NO = model.Eq_no,
                Eq_Type = model.Eq_Type,
                Eq_CertNo = model.Eq_CertNo,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_MadeNo = model.Eq_MadeNo,
                RegisterDate = model.CertDate,
                Id = model.Id,
                CreateDT = model.CreateDT
            };
        }
    }

    public class EquipmentDetailLocalModel : EquipmentPrintLocalModel
    {
        [Display(Name = "备注")]
        public String Comment;

        [Display(Name = "档案位置柜号")]
        [Required(ErrorMessage = "档案位置柜号不能为空")]
        public String Eq_RecDeskNo { get; set; }

        [Display(Name = "档案位置档案盒号")]
        [Required(ErrorMessage = "档案位置档案盒号不能为空")]
        public String Eq_RecBoxNo { get; set; }

        public static explicit operator EquipmentDetailLocalModel(Equipment model)
        {
            return new EquipmentDetailLocalModel()
            {
                OwnerCompanyName = model.Company.Name,
                OwnerCompanyAddress = model.Company.Address,
                OwnerCompanyInCharge = model.Eq_InCharge,
                OwnerCompanyPhone = model.Eq_Phone,
                ProducerCompany = model.Company1.Name,
                Eq_SpecCertNo = model.Eq_SpecCertNo,
                Name = model.EquipmentType.Name,
                NO = model.Eq_no,
                Eq_Type = model.Eq_Type,
                Eq_CertNo = model.Eq_CertNo,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_MadeNo = model.Eq_MadeNo,
                RegisterDate = model.CreateDT,
                Eq_Comment = model.Eq_Comment,
                Eq_RecBoxNo = model.Eq_RecBoxNo,
                Eq_RecDeskNo = model.Eq_RecDeskNo,
                Id = model.Id
            };
        }
    }

    public class EquipmentDetailExternalModel : EquipmentPrintExternalModel
    {
        [Display(Name = "备注")]
        public String Comment;

        [Display(Name = "档案位置柜号")]
        [Required(ErrorMessage = "档案位置柜号不能为空")]
        public String Eq_RecDeskNo { get; set; }

        [Display(Name = "档案位置档案盒号")]
        [Required(ErrorMessage = "档案位置档案盒号不能为空")]
        public String Eq_RecBoxNo { get; set; }

        public static explicit operator EquipmentDetailExternalModel(Equipment model)
        {
            return new EquipmentDetailExternalModel()
            {
                Eq_Owner = model.Eq_Owner,
                Eq_OwnerInCharge = model.Eq_InCharge,
                Eq_OwnerPhone = model.Eq_Phone,
                Eq_Producer = model.Eq_Producer,
                Eq_DesignCompany = model.Eq_DesignCompany,
                Name = model.EquipmentType.Name,
                Eq_NO = model.Eq_no,
                Eq_Type = model.Eq_Type,
                Eq_CertNo = model.Eq_CertNo,
                Eq_MadeDate = model.Eq_MadeDate,
                Eq_MadeNo = model.Eq_MadeNo,
                RegisterDate = model.CertDate,
                Eq_Comment = model.Eq_Comment,
                Eq_RecBoxNo = model.Eq_RecBoxNo,
                Eq_RecDeskNo = model.Eq_RecDeskNo,
                Id = model.Id,
                CreateDT = model.CreateDT
            };
        }
    }

    public class EquipmentSearchModel
    {
        [Display(Name = "设备名称")]
        public String Name { get; set; }

        [Display(Name = "备案编号")]
        public String NO_Start { get; set; }

        [Display(Name = "——")]
        public String NO_End { get; set; }

        [Display(Name = "录入时间")]
        public String CreateDT_Start { get; set; }

        [Display(Name = "——")]
        public String CreateDT_End { get; set; }

        [Display(Name = "产权单位")]
        public String OwnerName { get; set; }

        [Display(Name = "设备型号")]
        public String EquipmentModel { get; set; }

        [Display(Name = "生产厂家")]
        public String ProductCompany { get; set; }

        [Display(Name = "合格证号")]
        public String CertNO { get; set; }

        [Display(Name = "出厂代码")]
        public String MadeNo { get; set; }

        [Display(Name = "发证时间")]
        public String RegisterDT_Start { get; set; }

        [Display(Name = "——")]
        public String RegisterDT_End { get; set; }

        [Display(Name = "使用年限")]
        public String LimitedYear_Start { get; set; }

        [Display(Name = "——")]
        public String LimitedYear_End { get; set; }

        [Display(Name = "正在作业的设备")]
        public bool IsWorking { get; set; }

        [Display(Name = "已拆除或未使用的设备")]
        public bool IsAvailable { get; set; }

        [Display(Name = "仅在超过设计年限的设备中查找")]
        public bool IsExpired { get; set; }
    }

    public class EquipmentEditModel
    {
        [Display(Name = "设备名称")]
        public String Name { get; set; }

        public int Id { get; set; }

        [Display(Name = "备案编号")]
        public String Eq_No { get; set; }

        [Display(Name = "报检号")]
        public String InspectNO { get; set; }

        [Display(Name = "设计年限")]
        public int LimitedYears { get; set; }

        public static explicit operator EquipmentEditModel(Equipment model)
        {
            return new EquipmentEditModel()
            {
                Id = model.Id,
                Eq_No = model.Eq_no,
                InspectNO = model.Eq_InspectNO,
                Name = model.EquipmentType.Name,
                LimitedYears = model.LimitedMonth / 12
            };
        }
    }

    public class EquipmentContactorModel
    {
        public int Id { get; set; }
        [Display(Name = "联系人")]
        public String Legal { get; set; }
        [Display(Name = "联系电话")]
        public String Phone { get; set; }

        public static explicit operator EquipmentContactorModel(Equipment model)
        {
            return new EquipmentContactorModel()
            {
                Id = model.Id,
                Legal = model.Eq_InCharge,
                Phone = model.Eq_Phone
            };
        }
    }

}